<html>
<head><meta charset="utf-8"><title>limiting AsRef inference · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/limiting.20AsRef.20inference.html">limiting AsRef inference</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="195457546"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/limiting%20AsRef%20inference/near/195457546" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/limiting.20AsRef.20inference.html#195457546">(Apr 27 2020 at 18:31)</a>:</h4>
<p>Hi folks, I've got a question for you. For a time at least, and I presume still today, adding new <code>AsRef</code> (and maybe <code>Into</code>, <code>Borrow</code>, etc) impls was a very common source of breakage out in the ecosystem. It disrupted inference when people wrote code like <code>let x = y.as_ref()</code>, which worked so long as there was a unique type, but starts to fail once there are more impls available.</p>
<p>I've been wondering whether this is sufficiently disruptive that we ought to look into changing it. The main thing I am imagining is some form of annotation on the <code>AsRef</code> trait that indicates that all type parameters must be inferred before it will permit trait matching to proceed. There are some details in "how inferred" those type parameters have to be that are worth pondering, but I'd like to side-step that for now in favor of the bigger question -- would this change be desirable? Disruptive? Too disruptive?</p>
<p>We actually have behavior like this already. In particular, we will never infer the <em>self-type</em> based on trait matching. So if you have <code>?T: AsRef&lt;X&gt;</code> for some unknown type <code>?T</code>, we stop, even if there  is only one type that implements <code>AsRef&lt;X&gt;</code> (and hence we <em>could</em> succeed).  The most straightforward change would extend this behavior beyond the self-type, therefore. It'd be fairly easy to target that only at Rust 2021 crates, and I imagine we could even handle macros in a relatively seamless fashion. It'd be harder to make good warnings and migrations, but let's assume for now that it's possible.</p>



<a name="195458862"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/limiting%20AsRef%20inference/near/195458862" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/limiting.20AsRef.20inference.html#195458862">(Apr 27 2020 at 18:43)</a>:</h4>
<p>relevant: <a href="https://github.com/rust-lang/rust/issues/62586" title="https://github.com/rust-lang/rust/issues/62586">https://github.com/rust-lang/rust/issues/62586</a></p>



<a name="195490514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/limiting%20AsRef%20inference/near/195490514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/limiting.20AsRef.20inference.html#195490514">(Apr 28 2020 at 00:23)</a>:</h4>
<p>We've also discussed a lint in these contexts - calling <code>.as_ref()</code> or <code>.into()</code> on a concrete type.</p>



<a name="201845808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/limiting%20AsRef%20inference/near/201845808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/limiting.20AsRef.20inference.html#201845808">(Jun 24 2020 at 13:21)</a>:</h4>
<p>Yes, that would be good. But it looks troublesome to write everything as <code>?T: AsRef&lt;X&gt;</code>, maybe we could make <code>T: AsRef&lt;X&gt;</code> to take the first <code>as_ref</code> matched with <code>!T: AsRef&lt;X&gt;</code> as one that is explicit one match only.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>